<?php

$strSQL = "SELECT * FROM tblProduct";
   
$strJoin = " WHERE ";

    if ($txtID){
        $strSQL .= $strJoin."tblProduct.ID = '$txtID'";
        $strJoin = " AND ";
    }
    if ($txtProduct){
        $strSQL .= $strJoin."tblProduct.Product LIKE '$txtProduct%'";
        $strJoin = " AND ";
    }
    //if ($txtDescription){
        //$strSQL .= $strJoin."tblProduct.Description = '$txtDescription'";
        //$strJoin = " AND ";
    //}
    if ($cboWeight){
        $strSQL .= $strJoin."tblProduct.Weight = '$cboWeight'";
        $strJoin = " AND ";
    }
    if ($txtOwnProductCode){
        $strSQL .= $strJoin."tblProduct.OwnProductCode LIKE '$txtOwnProductCode%'";
        $strJoin = " AND ";
    }
    if ($txtBarcode){
        $strSQL .= $strJoin."tblProduct.Barcode LIKE '$txtBarcode%'";
        $strJoin = " AND ";
    }
    if ($cboSupplier){
        $strSQL .= $strJoin."tblProduct.Supplier = '$cboSupplier'";
        $strJoin = " AND ";
    }
    if ($txtSupplierProductID){
        $strSQL .= $strJoin."tblProduct.SupplierProductID LIKE '$txtSupplierProductID%'";
        $strJoin = " AND ";
    }
    if ($cboCategory){
        $strSQL .= $strJoin."tblProduct.Category = '$cboCategory'";
        $strJoin = " AND ";
    }
    if ($txtQuantityPerUnit){
        $strSQL .= $strJoin."tblProduct.QuantityPerUnit = '$txtQuantityPerUnit'";
        $strJoin = " AND ";
    }
    if ($txtUnitPrice){
        $strSQL .= $strJoin."tblProduct.UnitPrice = '$txtUnitPrice'";
        $strJoin = " AND ";
    }		 
    if ($txtCostPrice){
        $strSQL .= $strJoin."tblProduct.CostPrice = '$txtCostPrice'";
        $strJoin = " AND ";
    }
    if ($cboVatRate){
        $strSQL .= $strJoin."tblProduct.VatRate = '$cboVatRate'";
        $strJoin = " AND ";
    }
    if (($txtUnitsInStock) or ($txtUnitsInStock == "0")){
        $strSQL .= $strJoin."tblProduct.UnitsInStock = '$txtUnitsInStock'";
        $strJoin = " AND ";
    }
    if (($txtUnitsOnOrder) or ($txtUnitsOnOrder == "0")){
        $strSQL .= $strJoin."tblProduct.UnitsOnOrder = '$txtUnitsOnOrder'";
        $strJoin = " AND ";
    }
    if (($txtReorderLevel) or ($txtReorderLevel == "0")){
        $strSQL .= $strJoin."tblProduct.ReorderLevel = '$txtReorderLevel'";
        $strJoin = " AND ";
    }

    if ($txtUploadedPicture){
        $strSQL .= $strJoin."tblProduct.UploadedPicture = '$txtUploadedPicture'";
        $strJoin = " AND ";
    }
	
	if($cboUploadedPicture == 1) {
		$strSQL .= $strJoin."tblProduct.UploadedPicture LIKE  'product%'";
        $strJoin = " AND ";
	}
	
	if($cboUploadedPicture == 2) {
		$strSQL .= $strJoin."tblProduct.UploadedPicture =  'none'";
        $strJoin = " AND ";
	}
	
	if($cboDiscontinued == 1) {
		$strSQL .= $strJoin."tblProduct.Discontinued =  '1'";
        $strJoin = " AND ";
	}
	
	if($cboDiscontinued == 2) {
		$strSQL .= $strJoin."tblProduct.Discontinued =  '0'";
        $strJoin = " AND ";
	}
	
	if($cboPromotionalItem == 1) {
		$strSQL .= $strJoin."tblProduct.PromotionalItem =  '1'";
        $strJoin = " AND ";
	}
	
	if($cboPromotionalItem == 2) {
		$strSQL .= $strJoin."tblProduct.PromotionalItem =  '0'";
        $strJoin = " AND ";
	}
	
	//Date Added
	if (($txtDateAdded) && ($txtDateAddedUpperLimit)){
        $strSQL .= $strJoin."tblProduct.DateAdded Between '".ReformatToMySqlDate($txtDateAdded)."' AND '".ReformatToMySqlDate($txtDateAddedUpperLimit)."'";
        $strJoin = " AND ";
    	
		}elseif ((!$$txtDateAdded) && ($txtDateAddedUpperLimit)){
            $strSQL .= $strJoin."tblProduct.DateAdded <= '".ReformatToMySqlDate($txtDateAddedUpperLimit)."'";
			$strJoin = " AND ";
			
    	}elseif (($txtDateAdded) && (!$txtDateAddedUpperLimit)){
            $strSQL .= $strJoin."tblProduct.DateAdded >= '".ReformatToMySqlDate($txtDateAdded)."'";
			$strJoin = " AND ";
    }
    
    //Date Updated
	if (($txtDateUpdated) && ($txtDateUpdatedUpperLimit)){
        $strSQL .= $strJoin."tblProduct.DateUpdated Between '".ReformatToMySqlDate($txtDateUpdated)."' AND '".ReformatToMySqlDate($txtDateUpdatedUpperLimit)."'";
        $strJoin = " AND ";
    	
		}elseif ((!$$txtDateUpdated) && ($txtDateUpdatedUpperLimit)){
            $strSQL .= $strJoin."tblProduct.DateUpdated <= '".ReformatToMySqlDate($txtDateUpdatedUpperLimit)."'";
			$strJoin = " AND ";
			
    	}elseif (($txtDateUpdated) && (!$txtDateUpdatedUpperLimit)){
            $strSQL .= $strJoin."tblProduct.DateUpdated >= '".ReformatToMySqlDate($txtDateUpdated)."'";
			$strJoin = " AND ";
    }
    
    //Updated By
    if ($_REQUEST['cboUpdatedBy']){
        $strSQL .= $strJoin."tblProduct.UpdatedBy = '".$_REQUEST['cboUpdatedBy']."'";
        $strJoin = " AND ";
    }

$strSQL .= " ORDER BY tblProduct.Product;";
//echo "<br><br>SQL&nbsp;".$strSQL;
    include (MODULE . "/action/actMySqlResults");
	
?>
